package com.ruoyi.cderp.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblStockNumAdjustSheetDetail;
import com.ruoyi.cderp.mapper.TblStockNumAdjustSheetMapper;
import com.ruoyi.cderp.domain.TblStockNumAdjustSheet;
import com.ruoyi.cderp.service.ITblStockNumAdjustSheetService;

/**
 * 库存数量调整单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class TblStockNumAdjustSheetServiceImpl implements ITblStockNumAdjustSheetService 
{
    @Autowired
    private TblStockNumAdjustSheetMapper tblStockNumAdjustSheetMapper;

    /**
     * 查询库存数量调整单
     * 
     * @param id 库存数量调整单主键
     * @return 库存数量调整单
     */
    @Override
    public TblStockNumAdjustSheet selectTblStockNumAdjustSheetById(String id)
    {
        return tblStockNumAdjustSheetMapper.selectTblStockNumAdjustSheetById(id);
    }

    /**
     * 查询库存数量调整单列表
     * 
     * @param tblStockNumAdjustSheet 库存数量调整单
     * @return 库存数量调整单
     */
    @Override
    public List<TblStockNumAdjustSheet> selectTblStockNumAdjustSheetList(TblStockNumAdjustSheet tblStockNumAdjustSheet)
    {
        return tblStockNumAdjustSheetMapper.selectTblStockNumAdjustSheetList(tblStockNumAdjustSheet);
    }

    /**
     * 新增库存数量调整单
     * 
     * @param tblStockNumAdjustSheet 库存数量调整单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblStockNumAdjustSheet(TblStockNumAdjustSheet tblStockNumAdjustSheet)
    {
        tblStockNumAdjustSheet.setCreateTime(DateUtils.getNowDate());
        int rows = tblStockNumAdjustSheetMapper.insertTblStockNumAdjustSheet(tblStockNumAdjustSheet);
        insertTblStockNumAdjustSheetDetail(tblStockNumAdjustSheet);
        return rows;
    }

    /**
     * 修改库存数量调整单
     * 
     * @param tblStockNumAdjustSheet 库存数量调整单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblStockNumAdjustSheet(TblStockNumAdjustSheet tblStockNumAdjustSheet)
    {
        tblStockNumAdjustSheet.setUpdateTime(DateUtils.getNowDate());
        tblStockNumAdjustSheetMapper.deleteTblStockNumAdjustSheetDetailBySheetId(tblStockNumAdjustSheet.getId());
        insertTblStockNumAdjustSheetDetail(tblStockNumAdjustSheet);
        return tblStockNumAdjustSheetMapper.updateTblStockNumAdjustSheet(tblStockNumAdjustSheet);
    }

    /**
     * 批量删除库存数量调整单
     * 
     * @param ids 需要删除的库存数量调整单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblStockNumAdjustSheetByIds(String[] ids)
    {
        tblStockNumAdjustSheetMapper.deleteTblStockNumAdjustSheetDetailBySheetIds(ids);
        return tblStockNumAdjustSheetMapper.deleteTblStockNumAdjustSheetByIds(ids);
    }

    /**
     * 删除库存数量调整单信息
     * 
     * @param id 库存数量调整单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblStockNumAdjustSheetById(String id)
    {
        tblStockNumAdjustSheetMapper.deleteTblStockNumAdjustSheetDetailBySheetId(id);
        return tblStockNumAdjustSheetMapper.deleteTblStockNumAdjustSheetById(id);
    }

    /**
     * 新增库存数量调整单明细信息
     * 
     * @param tblStockNumAdjustSheet 库存数量调整单对象
     */
    public void insertTblStockNumAdjustSheetDetail(TblStockNumAdjustSheet tblStockNumAdjustSheet)
    {
        List<TblStockNumAdjustSheetDetail> tblStockNumAdjustSheetDetailList = tblStockNumAdjustSheet.getTblStockNumAdjustSheetDetailList();
        String id = tblStockNumAdjustSheet.getId();
        if (StringUtils.isNotNull(tblStockNumAdjustSheetDetailList))
        {
            List<TblStockNumAdjustSheetDetail> list = new ArrayList<TblStockNumAdjustSheetDetail>();
            for (TblStockNumAdjustSheetDetail tblStockNumAdjustSheetDetail : tblStockNumAdjustSheetDetailList)
            {
                tblStockNumAdjustSheetDetail.setSheetId(id);
                list.add(tblStockNumAdjustSheetDetail);
            }
            if (list.size() > 0)
            {
                tblStockNumAdjustSheetMapper.batchTblStockNumAdjustSheetDetail(list);
            }
        }
    }
}
